package com.mr.web.app;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;

import com.mr.biz.CategoryRepository;
import com.mr.biz.ProductRepository;
import com.mr.biz.support.Response;
import com.mr.utils.JacksonUtils;
import com.mr.web.support.AbstractApp;

@Component
public class ProductsApp extends AbstractApp {

	@Resource
	private ProductRepository productRepository;
	
	@Resource
	private CategoryRepository categoryRepository;

	public void load(ModelMap model, String id) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		Response response = productRepository.loadByCategory(generateRequestFromMap(map));
		model.addAttribute("products", JacksonUtils.readFromStr(response.getJsonData()));
		Response categories = categoryRepository.load();
		model.addAttribute("categories", JacksonUtils.readFromStr(categories.getJsonData()));
	}
}
